﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class Solution142
{
    public ListNode DetectCycle(ListNode head)
    {


       

        ListNode low = head;

        ListNode fast = head;

        while (fast != null && fast.next != null)
        {


            low = low.next;
            fast = fast.next.next;
            if (fast == low)
            {
                break;
            }
        }
        if(fast == null || fast.next == null)//无环
        {
            return null;
        }


       fast = head;

        while(low!=null&&fast != low)
        {
            fast = fast.next;
            low = low.next;
        }


        return low;






    }
}

